#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
使用requests请求代理服务器
请求http和https网页均适用
"""

import requests


class Proxies:

    def get_proxies(self):
        # # 要访问的目标网页
        # target_url = "https://www.baidu.com/"
        # 提取代理API接口，获取1个代理IP
        # api_url = "https://kps.kdlapi.com/api/getkps/?secret_id=ovdsqqk9slg6yiytdoy4&num=1&signature=35tb3vg3irc524h1gw6cg34gfdt93esl&pt=1&sep=1"
        api_url = "https://dps.kdlapi.com/api/getdps/?secret_id=o3jw67niqbj5ibal6aac&num=1&signature=xw3nwhfj178p1t3z00glrhiej47j7qz9&pt=1&sep=1"

        # 获取API接口返回的代理IP
        proxy_ip = requests.get(api_url).text
        # print(proxy_ip)
        # 用户名密码认证(私密代理/独享代理)
        # username = "zczhaod"
        # password = "zzcyyt05081129"
        username = "d3827482667"
        password = "zzcyyt05081129"
        proxies = {
            "http": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": proxy_ip},
            "https": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": proxy_ip}
        }
        # print(proxies)
        return proxies

        # 白名单方式（需提前设置白名单）
        # proxies = {
        #     "http": "http://%(proxy)s/" % {"proxy": proxy_ip},
        #     "https": "http://%(proxy)s/" % {"proxy": proxy_ip}
        # }

        # # 使用代理IP发送请求
        # response = requests.get(target_url, proxies=proxies)
        # print(response.status_code)
        #
        # if response.status_code == 200:
        #     response.encoding = "utf-8"
        #     print(response.text)

# proxies = Proxies()
# proxies.get_proxies()